package com.quanshu.shop.main.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.bumptech.glide.Glide;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.config.Config;
import com.quanshu.shop.base.tool.BaseTool;
import com.quanshu.shop.goods.view.SpecialGoodsActivity;
import com.quanshu.shop.main.entity.ShopMainEntity;

import java.util.List;

/**
 * 作者  李哲
 * 创建时间 2018/7/18
 * 邮箱 lizhetongliao@163.com
 * 说明
 **/
public class HotGoodsAdapter extends DelegateAdapter.Adapter<HotGoodsAdapter.HotGoodsAdapterViewHolder> {
    private Context mContext;
    private List<ShopMainEntity.HotsEntity> hotsEntityList;
    private LayoutHelper layoutHelper;
    public static final int HOT_GOODS_TYPE = 6;

    public HotGoodsAdapter(Context mContext, List<ShopMainEntity.HotsEntity> hotsEntityList, LayoutHelper layoutHelper) {
        this.mContext = mContext;
        this.hotsEntityList = hotsEntityList;
        this.layoutHelper = layoutHelper;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return layoutHelper;
    }

    @NonNull
    @Override
    public HotGoodsAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == HOT_GOODS_TYPE) {
            return new HotGoodsAdapterViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_shop_main_hot_goods, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull HotGoodsAdapterViewHolder holder, int position) {
        Glide.with(mContext).load(Config.URL_HOST_SHOP + hotsEntityList.get(position).getMob_headpic()).into(holder.imageView);
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString(SpecialGoodsActivity.SPECIAL_ID, hotsEntityList.get(position).getId());
                BaseTool.goActivity(mContext, SpecialGoodsActivity.class, bundle);
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        return HOT_GOODS_TYPE;
    }

    @Override
    public int getItemCount() {
        return hotsEntityList != null ? hotsEntityList.size() : 0;
    }

    class HotGoodsAdapterViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        public HotGoodsAdapterViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.shop_hot_goods_img);
        }
    }
}
